package com.ztman.rxjava.mvp.presenter;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ztman.rxjava.BaseFragment;
import com.ztman.rxjava.mvp.view.BaseViewActivityIml;

/**
 * Created by YJH on 2017/11/30.
 * 功能：针对 Fragment
 * 抽象出来的视图层，逻辑功能处理的代理
 */

public abstract class FragmentPresenter<T extends BaseViewActivityIml> extends BaseFragment {
    protected T fmtPresenter;

    /**
     * 功能：这个是关键，这个方法会在终端activity类中实现；从而激活所有的方法
     * @return
     */
    protected abstract Class<T> getPresenterClass();
    public FragmentPresenter(){
        try {
            fmtPresenter = getPresenterClass().newInstance();
        } catch (java.lang.InstantiationException e) {
            throw new RuntimeException("create BaseViewActivityIml error in FragmentPresenter.");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create BaseViewActivityIml error in FragmentPresenter.");
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fmtPresenter.InitWidgetInIBaseView();
    }



    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (fmtPresenter == null) {
            try {
                fmtPresenter = getPresenterClass().newInstance();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fmtPresenter = null;
    }
}
